package inventoryservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/utils/result"

	"property_service/property_service"
	"property_service/utils/BeanUtils"
)

type InventoryAssetCheckoutAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewInventoryAssetCheckoutAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InventoryAssetCheckoutAddLogic {
	return &InventoryAssetCheckoutAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// InventoryAssetCheckoutAdd 创建资产出库表
func (l *InventoryAssetCheckoutAddLogic) InventoryAssetCheckoutAdd(in *property_service.InventoryAssetCheckout) (*property_service.ResponseId, error) {
	// 获取资产出库表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).InventoryAssetCheckout
	// 创建一个新的资产出库表模型对象，不包含ID、TenantID、CreatedAt、UpdatedAt字段
	mod := &model.InventoryAssetCheckout{
		CheckoutCode:     in.CheckoutCode,                          // 出库单号
		CheckoutDate:     BeanUtils.GetDateFormat(in.CheckoutDate), // 出库日期
		Warehouse:        in.Warehouse,                             // 仓库
		Preparer:         in.Preparer,                              // 制单人
		WarehouseManager: in.WarehouseManager,                      // 仓管员
		UsingDept:        in.UsingDept,                             // 领用科室
		UsingDeptID:      in.UsingDeptID,                           // 领用科室ID
		Receiver:         in.Receiver,                              // 领用人
		ReceiverID:       in.ReceiverID,                            // 领用人ID
		Remarks:          in.Remarks,                               // 备注
		StatusID:         in.StatusID,                              // 状态ID
		StatusName:       in.StatusName,                            // 采购状态
		CheckoutStatus:   in.CheckoutStatus,                        // 状态
		StockID:          in.StockID,                               // 仓库ID
		TenantID:         in.TenantID,                              // 租户ID
		CreatedAtuser:    in.CreatedAtuser,                         // 创建人
		UpdatedAtuser:    in.UpdatedAtuser,                         // 更新人
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		res, err := result.Err(err).Ed()
		return l.buildResponse(mod.ID, res, err)
	}
	// 创建成功，返回成功响应
	res, err := result.Suc().AddEd()
	return l.buildResponse(mod.ID, res, err)
}

// 在 InventoryAssetCheckoutAddLogic 结构体中添加辅助方法
func (l *InventoryAssetCheckoutAddLogic) buildResponse(id int64, res *property_service.Response, err error) (*property_service.ResponseId, error) {
	return &property_service.ResponseId{
		Id:      id,
		Code:    res.Code,
		Message: res.Message,
	}, err
}
